package com.example.search.service.impl;

import com.example.search.dao.ShopRepository;
import com.example.search.model.Shop;
import com.example.search.service.IShopService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Auther: nanhuanh
 * @Date: 2019/3/6 16:08
 * @Description:
 */
@Slf4j
@Service
public class ShopServiceImpl implements IShopService {

    @Autowired
    @Qualifier("shopRepository")
    private ShopRepository shopRepository;


    @Override
    public Optional<Shop> findById(String id) {
        Optional<Shop> optionalShop = null;
        try {
            optionalShop = shopRepository.findById(id);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return optionalShop;
    }

    @Override
    public Shop save(Shop blog) {
        Shop shop = null;
        try {
            shop = shopRepository.save(blog);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shop;
    }

    @Override
    public void delete(Shop blog) {
        try {
            shopRepository.delete(blog);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
    }

    @Override
    public Shop update(Shop blog) {
        Shop tmp = null;
        try {
            if (StringUtils.isBlank(blog.getId())) {
                return null;
            }
            Optional<Shop> optional = findById(blog.getId());
            if (optional != null) {
                tmp = optional.get();
            }
            if (tmp != null) {
                BeanUtils.copyProperties(blog, tmp);
                shopRepository.save(tmp);
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return tmp;
    }

    @Override
    public Optional<Shop> findOne(String id) {
        Optional<Shop> optionalShop = null;
        try {
            optionalShop = shopRepository.findById(id);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return optionalShop;
    }

    @Override
    public Iterable<Shop> findAll() {
        Iterable<Shop> shops = null;
        try {
            shops = shopRepository.findAll();
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shops;
    }

    @Override
    public Page<Shop> findByShopName(String shopName, Pageable pageable) {
        Page<Shop> shopPage = null;
        try {
            shopPage = shopRepository.findByShopName(shopName, pageable);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shopPage;
    }

    @Override
    public Page<Shop> findByShopCode(String shopCode, Pageable pageable) {
        Page<Shop> shopPage = null;
        try {
            shopPage = shopRepository.findByShopCode(shopCode, pageable);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shopPage;
    }

    @Override
    public Page<Shop> findByIsOpen(int isOpen, Pageable pageable) {
        Page<Shop> shopPage = null;
        try {
            shopPage = shopRepository.findByIsOpen(isOpen, pageable);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shopPage;
    }
}
